﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Utility
{
    /// <summary>
    /// 字符串转换类
    /// </summary>
    public static class CharConversion
    {
        /// <summary>
        /// 字节数组转换为十进制
        /// </summary>
        /// <param name="bytedata"></param>
        /// <returns></returns>
        public static int ByteArrayToInt(byte[] bytedata)
        {
            StringBuilder stringBuilder = new StringBuilder(bytedata.Length);
            foreach (byte Byte in bytedata)
            {
                stringBuilder.Append(Byte.ToString("X2"));
            }
            int result = Convert.ToInt32(stringBuilder.ToString(), 16);
            return result;
            
        }
        /// <summary>
        /// 字节数组转换为字符串
        /// </summary>
        /// <param name="bytedata"></param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] bytedata)
        {
            
            string result = ByteArrayToString(bytedata, System.Text.Encoding.Default);
            return result;
        }

        public static string ByteArrayToString(byte[] bytedata, Encoding encoding)
        {

            string result = encoding.GetString(bytedata);
            return result;
        }

        /// <summary>
        /// 字符数组转换为16进制字符串
        /// </summary>
        /// <param name="bytedata"></param>
        /// <returns></returns>
        public static string ByteArrayToHexString(byte[] bytedata)
        {
            StringBuilder stringBuilder = new StringBuilder(bytedata.Length);
            foreach (byte Byte in bytedata)
            {
                stringBuilder.Append(Byte.ToString("X2"));
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 返回16进制16倍的数据源数据,用后面的余字节
        /// </summary>
        /// <param name="datastring"></param>
        /// <returns></returns>
        public static string StringToHexstring(string datastring)
        {
            byte[] bytetemp = System.Text.Encoding.Default.GetBytes(datastring); //字符串转化为字节数组
            string Hexdata = ByteArrayToHexString(bytetemp);
            int length = Hexdata.Length % 16;
            int j = (16 - length) / 2;
            string Data = string.Empty;
            for (int i = 0; i < 16 - length; i = i + 2)
            {
                Data += string.Format("0{0}", j);
            }
            Hexdata = Hexdata + Data;
            return Hexdata;
        }



        /// <summary>
        /// 字符串转换为字节数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string str)
        {
            byte[] resultArray = StringToByteArray(str, System.Text.Encoding.Default);//System.Text.Encoding.Default.GetBytes(str);
            return resultArray;
        }

        public  static byte[] StringToByteArray(string str,Encoding encoding)
        {
            byte[] resultArray = encoding.GetBytes(str);
            return resultArray;
        }


        /// <summary>
        /// 合并字符
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static byte[] HexStrToHexBuf(string hexString)
        {
            int srclen = hexString.Length;
            if (srclen % 2 != 0)
            {
                return null;
            }
            int len = srclen / 2;
            int indexOfArray = -1;
            byte[] hexadecimalArray = new byte[len];
            for (int index = 0; index < len; index++)
            {
                string value = hexString.Substring(index * 2, 2);
                hexadecimalArray[++indexOfArray] = Convert.ToByte(value.ToUpper(), 16);
            }
            return hexadecimalArray;
        }
        /// <summary>
        /// 16进制字符串转换为字节数组
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static byte[] HexStringToHexByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "");//移除空格
            byte[] comBuffer = new byte[hexString.Length / 2];
            for (int i = 0; i < hexString.Length; i += 2)
            {
                comBuffer[i / 2] = (byte)Convert.ToByte(hexString.Substring(i, 2), 16);
            }
            return comBuffer;
        }

        /// <summary>
        /// 将数组逆序
        /// </summary>
        /// <param name="bytedata"></param>
        /// <returns></returns>
        public static byte[] HexbyteToHexBuf(byte[] bytedata)
        {
            Array.Reverse(bytedata);
            return bytedata;
        }

        /// <summary>
        /// 16进制字符串转汉字或其他
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static String HexStringToString(string hexString)
        {
            byte[] tempArray = HexStringToHexByteArray(hexString);
            string result = ByteArrayToString(tempArray);
            return result;
        }

        /// <summary>
        /// 16进制字符串转汉字或其他
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static String HexStringToString(string hexString,Encoding encoding)
        {
            byte[] tempArray = HexStringToHexByteArray(hexString);
            string result = ByteArrayToString(tempArray, encoding);
            return result;
        }

        /// <summary>
        /// 字符串转16进制字符串
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static String StringToHexString(string msg)
        {
            byte [] tempArray = StringToByteArray(msg);
            string result = ByteArrayToHexString(tempArray);
            return result;
        }

        /// <summary>
        /// 字符串转16进制字符串
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static String StringToHexString(string msg,Encoding encoding)
        {
            byte[] tempArray = StringToByteArray(msg,encoding);
            string result = ByteArrayToHexString(tempArray);
            return result;
        }

        /// <summary>
        /// 00 00 00 05 ->5
        /// </summary>
        /// <param name="src"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static int BytesToInt(byte[] src, int offset)
        {
            int value;
            value = (int)(((src[offset] & 0xFF) << 24)
                    | ((src[offset + 1] & 0xFF) << 16)
                    | ((src[offset + 2] & 0xFF) << 8)
                    | (src[offset + 3] & 0xFF));
            return value;
        }
    }
}
